<template>
    <Modal title="公共分享" v-model="show" :closable="false" :mask-closable="false" @on-visible-change="showModal" :width="800">
        <el-form label-width="120px" v-model="formInfo">
            <el-form-item label="共享状态" prop="sharePublic">
                <el-radio-group v-model="formInfo.sharePublic">
                    <el-radio label="1">开启</el-radio>
                    <el-radio label="0">关闭</el-radio>
                </el-radio-group>
            </el-form-item>
            <template v-if="formInfo.sharePublic==1">
            <el-form-item label="共享价格" prop="price">
                <el-input v-model="formInfo.price" style="width: 120px" placeholder="共享价格" type="number" />
            </el-form-item>
            <el-form-item label="共享库存" prop="count">
                <el-input v-model="formInfo.count" placeholder="共享库存" style="width: 120px" type="number" />
            </el-form-item>
            </template>
        </el-form>
        <div slot="footer">
            <el-button size="small" @click="cancel" style="float: left;">取消</el-button>
            <el-button size="small" type="success" :loading="loading" @click="submit">保存</el-button>
        </div>
    </Modal>
</template>
<script>
export default {
    name: 'CSharePublicDialog',
    data() {
        return {
            formInfo: {
                price: 0,
                sharePublic:1
            },
            show: false,
            loading:false
        }
    },
    props: [
        'product'
    ],
    watch:{
    	'product': {
    		 handler(newval, olderval) {
                // this.formInfo.sharePublic=newval.sharePublic;
             }
    	}
    },
    methods: {
        cancel: function() {
            this.show = false;
        },
        showDialog() {
            this.show = true;
        },
        showModal(val) {
            if (val) {
                this.formInfo.price=this.product.salePrice;
                this.formInfo.sharePublic=this.product.sharePublic;
            } else {

            }
        },
        submit:function(){
           
            this.loading=true;
            this.$http.post("/storageproduct/doShare.do",{
                productId:this.product.id,
                price:this.formInfo.price,
                count:this.formInfo.count,
                sharePublic:this.formInfo.sharePublic
            }).then(response=>{
                 this.loading=false;
                if(response.status==0)
                {
                    this.show=false;
                    this.$emit("ok");
                }
                else{
                   this.errorMessage(response.info)
                }
            },response=>{
                   this.loading=false;
                this.errorMessage("系统异常")
            });
        }
    }
}
</script>